package com.zhiwen.controller;

import com.zhiwen.setannotation.DemoAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class AnalyzeController {

    public static void main(String[] args) {

        try {
            //使用类加载器加载类
            Class<?> aClass = Class.forName("com.zhiwen.service.AnnotationTest");
            //判断是否是存在这个注解
            boolean annotationPresent = aClass.isAnnotationPresent(DemoAnnotation.class);
            //获取注解实例
            //DemoAnnotation annotation1 = aClass.getAnnotation(DemoAnnotation.class);
            if (annotationPresent) {
                Annotation[] annotations = aClass.getAnnotations();
                //判断注解长度
                System.out.println(annotations.length);
                for (Annotation annotation : annotations) {
                    //获取实例
                    DemoAnnotation annotation1 = (DemoAnnotation) annotation;
                    System.out.println(annotation1.age());
                    System.out.println(annotation1.desc());
                    System.out.println(annotation1.author());
                }
            }

            Method[] methods = aClass.getMethods();
            for (Method method : methods) {
                boolean annotationPresent1 = method.isAnnotationPresent(DemoAnnotation.class);
                if (annotationPresent1) {
                    DemoAnnotation annotation1 = method.getAnnotation(DemoAnnotation.class);
                    System.out.println(annotation1.age());
                    System.out.println(annotation1.desc());
                    System.out.println(annotation1.author());
                }
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
